import {
	Properties
} from "../Thing/Properties.js"
import {
	Graphics
} from "../Thing/Graphics.js"
import {
	MaterialHelper
} from "./MaterialHelper.js"

class GraphicsHelper {
	constructor(arg) {}
	static create(options) {
		let _graphics = Cesium.defaultValue(options.graphics, {});
		let _position = _graphics.position;
		let _orientation = _graphics.position;
		let _label = _graphics.label;
		let _billboard = _graphics.billboard;
		let _point = _graphics.point;
		let _box = _graphics.box;
		let _corridor = _graphics.corridor;
		let _cylinder = _graphics.cylinder;
		let _ellipse = _graphics.ellipse;
		let _ellipsoid = _graphics.ellipsoid;
		let _model = _graphics.model;
		let _path = _graphics.path;
		let _plane = _graphics.plane;
		let _polygon = _graphics.polygon;
		let _polyline = _graphics.polyline;
		let _properties = _graphics.properties;
		let _polylineVolume = _graphics.polylineVolume;
		let _rectangle = _graphics.rectangle;
		let _wall = _graphics.wall;
		let graphics = {};

		if (Cesium.defined(_position)) {
			if (_position instanceof Cesium.CallbackProperty) {
				graphics.position = _position;
			} else {
				graphics.position = Cesium.Cartesian3.fromDegrees(_position.x, _position.y, _position.z)
			}
		}
		//point
		if (Cesium.defined(_point)) {
			graphics.point={}
			this.createPoint(graphics.point,_point);
		}
		
		//label
		if (Cesium.defined(_label)) {
			graphics.label={};
			this.createLabel(graphics.label,_label);
		}
		
		//billboard
		if (Cesium.defined(_billboard)) {
			graphics.billboard={};
			this.createBillboard(graphics.billboard,_billboard);
		}
		if (Cesium.defined(_properties)) {
			graphics.properties = this.createProperties(_properties);
		}
		if (Cesium.defined(_polyline)) {
			graphics.polyline={};
			this.createPolyline(graphics.polyline,_polyline);
		}
		if (Cesium.defined(_polygon)) {
			graphics.polygon={}
			this.createPolygon(graphics.polygon,_polygon);
		}

		return new Graphics({
			group: Cesium.defaultValue(options.group, {}),
			graphics: graphics,
			attributes: Cesium.defaultValue(options.attributes, {}),
		});
	}

	/**
	 * @description 点
	 * @param {Object} _point
	 */
	static createPoint(point,_point) {
		if (Cesium.defined(_point.show)) {
			point.show = _point.show;
		}
		if (Cesium.defined(_point.size)) {
			point.pixelSize = _point.size;
		}
		if (Cesium.defined(_point.heightReference)) {
			point.heightReference = _point.heightReference;
		}
		if (Cesium.defined(_point.color)) {
			point.color = Cesium.Color.fromCssColorString(_point.color)
		}
		if (Cesium.defined(_point.outline)){
			if (Cesium.defined(_point.outline.color)) {
				point.outlineColor = Cesium.Color.fromCssColorString(_point.outline.color);
			}
			if (Cesium.defined(_point.outline.width)) {
				point.outlineWidth = _point.outline.width;
			}
		}
		if (Cesium.defined(_point.scaleByDistance)) {
			point.scaleByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_point.scaleByDistance.near, 0),
				Cesium.defaultValue(_point.scaleByDistance.nearValue, 0),
				Cesium.defaultValue(_point.scaleByDistance.far, 1),
				Cesium.defaultValue(_point.scaleByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_point.translucencyByDistance)) {
			point.translucencyByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_point.translucencyByDistance.near, 0),
				Cesium.defaultValue(_point.translucencyByDistance.nearValue, 0),
				Cesium.defaultValue(_point.translucencyByDistance.far, 1),
				Cesium.defaultValue(_point.translucencyByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_point.distanceDisplay)) {
			point.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				Cesium.defaultValue(_point.distanceDisplay.near, 0),
				Cesium.defaultValue(_point.distanceDisplay.far, Number.MAX_VALUE)
			);
		}
		if (Cesium.defined(_point.disableDepthTestDistance)) {
			point.disableDepthTestDistance = _point.disableDepthTestDistance
		}
	}

	/**
	 * @description 标签
	 * @param {Object} _label
	 */
	static createLabel(label,_label) {
		if (Cesium.defined(_label.show)) {
			label.show = _label.show;
		}
		if (Cesium.defined(_label.scale)) {
			label.scale = _label.scale;
		}
		if (Cesium.defined(_label.text)) {
			label.text = _label.text;
		}
		
		if (Cesium.defined(_label.color)) {
			label.fillColor = Cesium.Color.fromCssColorString(_label.color);
		}
		
		if (Cesium.defined(_label.style)) {
			label.style = _label.style;
		}
		
		if(Cesium.defined(_label.font)){
			label.font = _label.font;
		}
		
		if(Cesium.defined(_label.background)){
			if (Cesium.defined(_label.background.show)) {
				label.showBackground = _label.background.show
			}
			if (Cesium.defined(_label.background.color)) {
				label.backgroundColor = Cesium.Color.fromCssColorString(_label.background.color)
			}
			if (Cesium.defined(_label.background.padding)) {
				label.backgroundPadding = new Cesium.Cartesian2(
					Cesium.defaultValue(_label.background.padding.x, 7),
					Cesium.defaultValue(_label.background.padding.y, 5)
				);
			}
		}
		
		if (Cesium.defined(_label.pixelOffset)) {
			label.pixelOffset = new Cesium.Cartesian2(
				Cesium.defaultValue(_label.pixelOffset.x, 0),
				Cesium.defaultValue(_label.pixelOffset.y, 0)
			);
		}
		
		if (Cesium.defined(_label.eyeOffset)) {
			label.eyeOffset = new Cesium.Cartesian3(
				Cesium.defaultValue(_label.eyeOffset.x, 0),
				Cesium.defaultValue(_label.eyeOffset.y, 0),
				Cesium.defaultValue(_label.eyeOffset.z, 0)
			);
		}
		if (Cesium.defined(_label.horizontal)) {
			label.horizontalOrigin = _label.horizontal;
		}
		if (Cesium.defined(_label.vertical)) {
			label.verticalOrigin = _label.vertical;
		}
		if (Cesium.defined(_label.heightReference)) {
			label.heightReference = _label.heightReference;
		}
		
		
		if (Cesium.defined(_label.outline)){
			if (Cesium.defined(_label.outline.color)) {
				label.outlineColor = Cesium.Color.fromCssColorString(_label.outline.color);
			}
			if (Cesium.defined(_label.outline.width)) {
				label.outlineWidth = _label.outline.width;
			}
		}
		
		if (Cesium.defined(_label.translucencyByDistance)) {
			label.translucencyByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_label.translucencyByDistance.near, 0),
				Cesium.defaultValue(_label.translucencyByDistance.nearValue, 0),
				Cesium.defaultValue(_label.translucencyByDistance.far, 1),
				Cesium.defaultValue(_label.translucencyByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_label.pixelOffsetScaleByDistance)) {
			label.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_label.pixelOffsetScaleByDistance.near, 0),
				Cesium.defaultValue(_label.pixelOffsetScaleByDistance.nearValue, 0),
				Cesium.defaultValue(_label.pixelOffsetScaleByDistance.far, 1),
				Cesium.defaultValue(_label.pixelOffsetScaleByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_label.scaleByDistance)) {
			label.scaleByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_label.scaleByDistance.near, 0),
				Cesium.defaultValue(_label.scaleByDistance.nearValue, 0),
				Cesium.defaultValue(_label.scaleByDistance.far, 1),
				Cesium.defaultValue(_label.scaleByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_label.distanceDisplay)) {
			label.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				Cesium.defaultValue(_label.distanceDisplay.near, 0),
				Cesium.defaultValue(_label.distanceDisplay.far, Number.MAX_VALUE)
			);
		}
		if (Cesium.defined(_label.disableDepthTestDistance)) {
			label.disableDepthTestDistance = _label.disableDepthTestDistance;
		}
	}

	/**
	 * @description 广告牌
	 * @param {Object} _billboard
	 */
	static createBillboard(billboard,_billboard) {
		if (Cesium.defined(_billboard.show)) {
			billboard.show = _billboard.show
		}
		if (Cesium.defined(_billboard.image)) {
			if(Cesium.defined(_billboard.image.url)){
				billboard.image = _billboard.image.url
			}
			if (Cesium.defined(_billboard.image.width)) {
				billboard.width = _billboard.image.width
			}
			if (Cesium.defined(_billboard.image.height)) {
				billboard.height = _billboard.height
			}
		}
		if (Cesium.defined(_billboard.scale)) {
			billboard.scale = _billboard.scale
		}
		if (Cesium.defined(_billboard.pixelOffset)) {
			billboard.pixelOffset = new Cesium.Cartesian2(
				Cesium.defaultValue(_billboard.pixelOffset.x, 0),
				Cesium.defaultValue(_billboard.pixelOffset.y, 0)
			);
		}
		if (Cesium.defined(_billboard.eyeOffset)) {
			billboard.eyeOffset = new Cesium.Cartesian3(
				Cesium.defaultValue(_billboard.eyeOffset.x, 0),
				Cesium.defaultValue(_billboard.eyeOffset.y, 0),
				Cesium.defaultValue(_billboard.eyeOffset.z, 0)
			);
		}

		if (Cesium.defined(_billboard.horizontal)) {
			billboard.horizontalOrigin = _billboard.horizontal;
		}
		if (Cesium.defined(_billboard.vertical)) {
			billboard.verticalOrigin = _billboard.vertical;
		}
		if (Cesium.defined(_billboard.heightReference)) {
			billboard.heightReference = _billboard.heightReference;
		}
		if (Cesium.defined(_billboard.color)) {
			billboard.color = Cesium.Color.fromCssColorString(_billboard.color);
		}
		if (Cesium.defined(_billboard.rotation)) {
			billboard.rotation = _billboard.rotation
		}
		if (Cesium.defined(_billboard.alignedAxis)) {
			billboard.alignedAxis = new Cesium.Cartesian3(
				Cesium.defaultValue(_billboard.alignedAxis.x, 0),
				Cesium.defaultValue(_billboard.alignedAxis.y, 0),
				Cesium.defaultValue(_billboard.alignedAxis.z, 0)
			)
		}
		if (Cesium.defined(_billboard.sizeInMeters)) {
			billboard.sizeInMeters = _billboard.sizeInMeters
		}

		if (Cesium.defined(_billboard.translucencyByDistance)) {
			billboard.translucencyByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_billboard.translucencyByDistance.near, 0),
				Cesium.defaultValue(_billboard.translucencyByDistance.nearValue, 0),
				Cesium.defaultValue(_billboard.translucencyByDistance.far, 1),
				Cesium.defaultValue(_billboard.translucencyByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_billboard.pixelOffsetScaleByDistance)) {
			billboard.pixelOffsetScaleByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_billboard.pixelOffsetScaleByDistance.near, 0),
				Cesium.defaultValue(_billboard.pixelOffsetScaleByDistance.nearValue, 0),
				Cesium.defaultValue(_billboard.pixelOffsetScaleByDistance.far, 1),
				Cesium.defaultValue(_billboard.pixelOffsetScaleByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_billboard.scaleByDistance)) {
			billboard.scaleByDistance = new Cesium.NearFarScalar(
				Cesium.defaultValue(_billboard.scaleByDistance.near, 0),
				Cesium.defaultValue(_billboard.scaleByDistance.nearValue, 0),
				Cesium.defaultValue(_billboard.scaleByDistance.far, 1),
				Cesium.defaultValue(_billboard.scaleByDistance.farValue, 0),
			);
		}
		if (Cesium.defined(_billboard.distanceDisplay)) {
			billboard.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				Cesium.defaultValue(_billboard.distanceDisplay.near, 0),
				Cesium.defaultValue(_billboard.distanceDisplay.far, Number.MAX_VALUE)
			);
		}
		if (Cesium.defined(_billboard.disableDepthTestDistance)) {
			billboard.disableDepthTestDistance = _billboard.disableDepthTestDistance;
		}
	}
	static createBox() {}
	static createCorridor() {}
	static createCylinder() {}
	static createEllipse() {}
	static createEllipsoid() {}
	static createModel() {}
	static createPath() {}
	static createPlane() {}

	/**
	 * @description 多边形
	 * @param {Object} _polygon
	 */
	static createPolygon(polygon,_polygon) {
		if (Cesium.defined(_polygon.show)) {
			polygon.show = _polygon.show
		}
		if (Cesium.defined(_polygon.positions)) {
			if (_polygon.positions instanceof Cesium.CallbackProperty) {
				polygon.hierarchy = _polygon.positions;
			} else {
				let hierarchy=[];
				for(var point of  _polygon.positions){
					hierarchy.push(Cesium.Cartesian3.fromDegrees(point.x*1,point.y*1,point.z*1));
				}
				polygon.hierarchy = hierarchy
			}
		}
		
		if (Cesium.defined(_polygon.height)) {
			polygon.height = _polygon.height
		}
		if (Cesium.defined(_polygon.heightReference)) {
			polygon.heightReference = _polygon.heightReference
		}
		if (Cesium.defined(_polygon.extrudedHeight)) {
			polygon.extrudedHeight = _polygon.extrudedHeight
		}
		if (Cesium.defined(_polygon.extrudedHeightReference)) {
			polygon.extrudedHeightReference = _polygon.extrudedHeightReference
		}
		if (Cesium.defined(_polygon.stRotation)) {
			polygon.stRotation = _polygon.stRotation
		}

		if (Cesium.defined(_polygon.granularity)) {
			polygon.granularity = _polygon.granularity
		}
		if (Cesium.defined(_polygon.fill)) {
			polygon.fill = _polygon.fill
		}
		if (Cesium.defined(_polygon.material)) {
			
			if(Cesium.defined(_polygon.material.type)=='imge')
				polygon.material =Cesium.Color.fromCssColorString(_polygon.material.color);
			else if(){
				polygon.material = new Cesium.Material({
				  fabric : {
				    type : 'DiffuseMap',
				    uniforms : {
				      image : 'diffuse.png',
				      repeat : {
				        x : 10,
				        y : 2
				      }
				    }
				  }
				});
			}
			else	
				polygon.material =Cesium.Color.WHITE
		}else{
			polygon.material =Cesium.Color.WHITE
		}
		
		//轮廓
		if (Cesium.defined(_polygon.outline)) {
			if(Cesium.defined(_polygon.outline.show)){
				polygon.outline = _polygon.outline.show
			}
			if (Cesium.defined(_polygon.outline.color)) {
				polygon.outlineColor = Cesium.Color.fromCssColorString(_polygon.outline.color)
			}
			if (Cesium.defined(_polygon.outline.width)) {
				polygon.outlineWidth = _polygon.outline.width
			}
		}
		
		if (Cesium.defined(_polygon.perPositionHeight)) {
			polygon.perPositionHeight = _polygon.perPositionHeight
		}
		if (Cesium.defined(_polygon.closeTop)) {
			polygon.closeTop = _polygon.closeTop
		}
		if (Cesium.defined(_polygon.closeBottom)) {
			polygon.closeBottom = _polygon.closeBottom
		}
		if (Cesium.defined(_polygon.arcType)) {
			polygon.arcType = _polygon.arcType
		}
		if (Cesium.defined(_polygon.shadows)) {
			polygon.shadows = _polygon.shadows
		}
		if (Cesium.defined(_polygon.classificationType)) {
			polygon.classificationType = _polygon.classificationType
		}
		if (Cesium.defined(_polygon.zIndex)) {
			polygon.zIndex = _polygon.zIndex
		}
		if (Cesium.defined(_polygon.distanceDisplay)) {
			polygon.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				Cesium.defaultValue(_polygon.distanceDisplay.near, 0),
				Cesium.defaultValue(_polygon.distanceDisplay.far, Number.MAX_VALUE)
			);
		}
	}

	/**
	 * @description 多线段
	 * @param {Object} _polyline
	 */
	static createPolyline(polyline,_polyline) {
		console.log(_polyline);
		if (Cesium.defined(_polyline.show)) {
			polyline.show = _polyline.show
		}
		if (Cesium.defined(_polyline.positions)) {
			polyline.positions = _polyline.positions
			if (Cesium.defined(_polyline.positions)) {
				if (_polyline.positions instanceof Cesium.CallbackProperty) {
					polyline.positions = _polyline.positions;
				} else {
					let positions=[];
					for(var point of  _polyline.positions){
						positions.push(Cesium.Cartesian3.fromDegrees(point.x*1,point.y*1,point.z*1));
					}
					polyline.positions=positions;
				}
			}
		}
		if (Cesium.defined(_polyline.width)) {
			polyline.width = _polyline.width
		}
		if (Cesium.defined(_polyline.granularity)) {
			polyline.granularity = _polyline.granularity
		}
		if (Cesium.defined(_polyline.material)) {
			if(Cesium.defined(_polyline.material.type)){
				if('glow'==_polyline.material.type){
					polyline.material =new Cesium.PolylineGlowMaterialProperty({
					  glowPower: Cesium.defaultValue(_polyline.material.glowPower, 0.2),
					  taperPower: Cesium.defaultValue(_polyline.material.taperPower, 0.5),
					  color: Cesium.Color.fromCssColorString(_polyline.material.color),
					})
				}
				else if('outline'==_polyline.material.type){
					polyline.material =new Cesium.PolylineOutlineMaterialProperty({
					  color: Cesium.Color.fromCssColorString(_polyline.material.color),
					  outlineWidth: Cesium.defaultValue(_polyline.material.outlineWidth, 1),
					  outlineColor: Cesium.Color.fromCssColorString(_polyline.material.outlineColor),
					})
				}
				else if('arrow'==_polyline.material.type){
					polyline.material =new Cesium.PolylineArrowMaterialProperty(
					  Cesium.Color.fromCssColorString(_polyline.material.color)
					)
				}
				else if('dash'==_polyline.material.type){
					 polyline.material =new Cesium.PolylineDashMaterialProperty({
					  color:Cesium.defined(_polyline.material.color)?Cesium.Color.fromCssColorString(_polyline.material.color):Cesium.Color.WHITE,
					  gapColor:Cesium.defined(_polyline.material.gapColor)? Cesium.Color.fromCssColorString(_polyline.material.gapColor):Cesium.Color.TRANSPARENT,
					  dashLength:Cesium.defined(_polyline.material.dashLength)?_polyline.material.dashLength:16,
					  dashPattern:Cesium.defined(_polyline.material.dashPattern)?_polyline.material.dashPattern:255,
					})
				}
				else{
					polyline.material =Cesium.Color.fromCssColorString(_polyline.material.color);
				}
			}else{
				if(Cesium.defined(_polyline.material.color))
					polyline.material =Cesium.Color.fromCssColorString(_polyline.material.color);
				else	
					polyline.material =Cesium.Color.WHITE
			}
		}
		if (Cesium.defined(_polyline.depthFailMaterial)) {
			polyline.depthFailMaterial = _polyline.depthFailMaterial
		}

		if (Cesium.defined(_polyline.arcType)) {
			polyline.arcType = _polyline.arcType
		}
		if (Cesium.defined(_polyline.clampToGround)) {
			polyline.clampToGround = _polyline.clampToGround
		}
		if (Cesium.defined(_polyline.shadows)) {
			polyline.shadows = _polyline.shadows
		}
		if (Cesium.defined(_polyline.classificationType)) {
			polyline.classificationType = _polyline.classificationType
		}
		if (Cesium.defined(_polyline.zIndex)) {
			polyline.zIndex = _polyline.zIndex
		}
		if (Cesium.defined(_polyline.distanceDisplay)) {
			polyline.distanceDisplayCondition = new Cesium.DistanceDisplayCondition(
				Cesium.defaultValue(_polyline.distanceDisplay.near, 0),
				Cesium.defaultValue(_polyline.distanceDisplay.far, Number.MAX_VALUE)
			);
		}
	}
	static createProperties(_properties) {
		let properties = new Properties();
		for (var propertyName in _properties) {
			properties.addProperty(propertyName, obj[propertyName])
		}
		return properties;
	}
	static createPolylineVolume() {}
	static createRectangle() {}
	static createWall() {}
}
export {
	GraphicsHelper
}
